The code on the right shows how we put the message into our program. We create a string and then give a pointer to the base of the string to the function lcd_print
, which sends out each byte of the string in turn. Some programming languages let you create variables of a string type, just as we can create integer, floating point and character variables. C does not let you do this, but it does let you treat a character array as a string. The convention in C is that a string is an array of characters terminated with a null character. A null character is one with with the value 0 (as opposed to the ASCII character represents the value 0).
The array message is set up as a string, with the characters 'h', 'e', 'l', 'l', and 'o'. I then put the value 0 to mark the end of the string. The function which prints the string will send out characters until it reaches a character with the value 0.
The function lcd_print has a single parameter, which is a pointer to the base of the string to be sent out. I can tell that it is a pointer because of the *
between char
and mess
. In this context C regards this as meaning "pointer to":
void lcd_print (
const unsigned char * mess )
Within the send message function I can treat mess as an array and access successive elements in memory from the base of the pointer. For more details on arrays and pointers consult the C reference section.
The program has been designed this way to make it easy to create and send new messages:
const unsigned char goodbye [8]
={'g','o','o','d','b','y','e',0x00};
This would set up a string which contains the message 'goodbye'. (Note that it is very important that you remember to put the 0 on the end to mark the limit of the string. If you miss this off the lcd_print
function will zoom down memory printing rubbish!)
When I want to say goodbye to the user I can make the call:
lcd_print ( goodbye ) ;
The lcd_print
function will now send out the contents of the goodbye array.
const unsigned char message [6] =
{
'h','e', 'l','l', 'o', 0x00
} ;
/* lcd_print accepts a pointer */
/* parameter which points at the */
/* string to be printed */
char lcd_print (
const unsigned char * mess )
{
unsigned char i = 0 ;
/* treat the pointer as an */
/* array and send bytes until */
/* we find an element with 0 */
/* in it */
while ( mess [i] != 0 )
{
lcd_print_ch ( mess[i] ) ;
i++ ;
}
return 1 ;
}